import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { CoreModule } from './../core.module';
import { StorageService } from './../sotrage/storage.service';
import { Router } from '@angular/router';
import { NzNotificationService, NzMessageService } from 'ng-zorro-antd';

@Injectable({
  providedIn: CoreModule
})
export class ApiService {

  constructor(
    private message: NzMessageService,// lee 添加
    private http: HttpClient,
    private storageService: StorageService,
    private router: Router,
    private notification: NzNotificationService
  ) { }

  get<T>(url: string, params: { [key: string]: string }): Observable<T> {
    return this.http.get<T>(url, {
      params: params
    });
  }

  post<T>(url: string, params?: any): Observable<T> {
    return this.http.post<T>(url, params);
  }

  put<T>(url: string, params: any): Observable<T> {
    return this.http.put<T>(url, params);
  }

  delete<T>(url: string, params: any): Observable<T> {
    return this.http.delete<T>(url, {
      params: params
    });
  }

  goToWhere(errorCode: any) {
    // this._alert.createNotification('error')
    switch (errorCode.status || errorCode) {
      case 403:
        this.router.navigate(['pages/login']);
        break;
      default:
        if (errorCode.message) {
          this.notification.create('error', errorCode.message, '错误提示');
        } else if (errorCode._body) {
          const error = JSON.parse(errorCode._body);
          this.notification.create('error', error['message'], '错误提示');
        }
        break;
    }
  };
  /**
   * 
   * @param data 
   * @param isShowSuccessMsg 是否显示成功信息{bool,msg}
   * 返回数据，如遇错误在此方法中
   * isShowSuccessMsg:
   */
  getData(data, isShowSuccessMsg):boolean {
    let bool;
    if(data.status){
      bool = false;
      this.message.create('error', data.message);
    }else{
      isShowSuccessMsg.bool ? this.message.create('success', isShowSuccessMsg.msg): null;
      bool = true;
    };
    return bool;
  };
};
